package com.mecol.library.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mecol.library.dao.LendDao;
import com.mecol.library.entity.Lend;
import com.mecol.library.entity.LendSearch;
import com.mecol.library.service.LendService;
import com.mecol.library.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LendServiceImpl implements LendService{

    @Autowired
    private LendDao lendDao;

    @Override
    public ResultUtil getAllLendList(Integer page, Integer limit, LendSearch search) {
        PageHelper.startPage(page,limit);
        List<Lend> lends=lendDao.getAllLendList(search);
        PageInfo<Lend> pageInfo=new PageInfo<>(lends);
        ResultUtil resultUtil=new ResultUtil();
        resultUtil.setCode(0);
        resultUtil.setCount(pageInfo.getTotal());
        resultUtil.setData(pageInfo.getList());
        return resultUtil;
    }

    @Override
    public Lend getLendById(int ser_num) {
        return lendDao.getLendById(ser_num);
    }

    @Override
    public void updateLend(Lend lend) {
        lendDao.updateLend(lend);
    }

    @Override
    public void deleteLendById(int ser_num) {
        lendDao.deleteLendById(ser_num);
    }

    @Override
    public void insertNewLend(Long book_id, Long user_id, Date lend_date) {
        lendDao.insertNewLend(book_id,user_id,lend_date);
    }

    @Override
    public ResultUtil getMyLendList(Integer page, Integer limit,Long user_id) {
        PageHelper.startPage(page,limit);
        List<Lend> lends=lendDao.getMyLendList(user_id);
        PageInfo<Lend> pageInfo=new PageInfo<>(lends);
        ResultUtil resultUtil=new ResultUtil();
        resultUtil.setCode(0);
        resultUtil.setCount(pageInfo.getTotal());
        resultUtil.setData(pageInfo.getList());
        return resultUtil;
    }

}
